package com.cloudkinto.appletservice.covershows.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.appletservice.covershows.AppletNewsInformationService;
import com.cloudkinto.appletservice.covershows.vo.AppletNewsInformationDetailRes;
import com.cloudkinto.appletservice.covershows.vo.AppletNewsInformationPageRes;
import com.cloudkinto.common.exception.BizException;
import com.cloudkinto.common.exception.BizExceptionI18;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.dao.NewsInformationDao;
import com.cloudkinto.entity.NewsInformationDo;
import com.cloudkinto.service.email.MailSenderService;
import com.cloudkinto.service.file.FileService;
import com.cloudkinto.service.region.JpRegionService;
import com.cloudkinto.service.storage.StorageService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * service实现类
 * </p>
 *
 * @author zhangyy
 * @since 2020-09-27
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class AppletNewsInformationServiceImpl extends ServiceImpl<NewsInformationDao, NewsInformationDo> implements AppletNewsInformationService {
    @Autowired
    private NewsInformationDao dao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private FileService fileService;
    @Autowired
    private StorageService storageService;
    @Autowired
    private JpRegionService jpRegionService;
    @Autowired
    private MailSenderService mailSenderService;
    @Autowired
    private ThreadPoolTaskExecutor executor;


    @Override
    public AppletNewsInformationDetailRes detail(Long id) {
        NewsInformationDo entityDo = dao.selectById(id);
        if (entityDo == null) throw new BizExceptionI18("no.order.info");
        entityDo.setBrowseCount(entityDo.getBrowseCount() + 1);
        entityDo.setCollectCount(entityDo.getCollectCount() + 1);
        dao.updateById(entityDo);
        AppletNewsInformationDetailRes res = new AppletNewsInformationDetailRes();
        BeanUtils.copyProperties(entityDo, res);
        res.setFileUrl(fileService.getFilePath(entityDo.getFileId()));
        return res;
    }


    @Override
    public List<AppletNewsInformationPageRes> getList(Map<String, String> map) {
        QueryWrapper<NewsInformationDo> wrapper = queryBuild(map);
        List<NewsInformationDo> list = this.list(wrapper);
        List<AppletNewsInformationPageRes> result = handlerListAfter(list);
        return result;
    }

    @Override
    public PageResult getListPage(Map<String, String> map) {
        QueryWrapper<NewsInformationDo> wrapper = queryBuild(map);
        IPage<NewsInformationDo> page = pageInit(map);
        page = this.page(page, wrapper);
        List<AppletNewsInformationPageRes> resList = handlerListAfter(page.getRecords());
        PageResult result = new PageResult(resList, (int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), (int) page.getPages());
        return result;
    }


    //查询分页列表之后对列表数据的处理
    private List<AppletNewsInformationPageRes> handlerListAfter(List<NewsInformationDo> list) {
        List<AppletNewsInformationPageRes> result = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(newsInformationDo -> {
                AppletNewsInformationPageRes res = new AppletNewsInformationPageRes();
                BeanUtils.copyProperties(newsInformationDo, res);
                res.setFileUrl(fileService.getFilePath(newsInformationDo.getFileId()));
                result.add(res);
            });
        }
        return result;
    }

    private IPage<NewsInformationDo> pageInit(Map<String, String> map) {
        IPage<NewsInformationDo> page = new Page();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 5);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }

    private QueryWrapper<NewsInformationDo> queryBuild(Map<String, String> map) {
        QueryWrapper<NewsInformationDo> wrapper = new QueryWrapper<>();
//        if (StringUtils.isNotBlank(map.get("keyword"))) {
//            wrapper.like("keyword", map.get("keyword"));
//        }
        return wrapper.orderByDesc("id");
    }


}
